package com.gitee.mocoffee.embedzk;

import java.io.File;

import org.apache.curator.test.TestingServer;

import com.google.common.base.Strings;

import lombok.AccessLevel;
import lombok.experimental.FieldDefaults;

/**
 * @author lijia
 */
@FieldDefaults(level = AccessLevel.PRIVATE)
public class EmbedZK {
    Integer port;
    String  filePath;

    volatile TestingServer server;

    public EmbedZK(Integer port, String filePath) throws Exception {
        int p = port != null && port > 0 ? port : -1;
        File tempF = Strings.isNullOrEmpty(filePath) ? null : new File(filePath);
        server = new TestingServer(p, tempF);
    }

    public EmbedZK(Integer port) throws Exception {
        this(port, null);
    }

    public EmbedZK(String filePath) throws Exception {
        this(null, filePath);
    }

    public EmbedZK() throws Exception {
        this(null, null);
    }

    public void start() throws Exception {
        if (server != null)
            server.start();
    }

    public void stop() throws Exception {
        if (server != null)
            server.stop();
    }
}
